package com.ljfiction.book.activitys;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ljfiction.book.Baner.TaskBean;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.Dialog.PhoneBangding;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.manage.CodeManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class TaskActivity extends BaseActivity {

    @BindView(R.id.taskgo1)
    SuperButton taskgo1;
    @BindView(R.id.taskgo2)
    SuperButton taskgo2;
    @BindView(R.id.taskgo3)
    SuperButton taskgo3;
    @BindView(R.id.taskgo4)
    SuperButton taskgo4;
    @BindView(R.id.taskgo5)
    SuperButton taskgo5;
    @BindView(R.id.taskgo6)
    SuperButton taskgo6;
    @BindView(R.id.taskgo7)
    SuperButton taskgo7;
    @BindView(R.id.taskgo8)
    SuperButton taskgo8;
    @BindView(R.id.taskgo9)
    SuperButton taskgo9;
    @BindView(R.id.taskgo10)
    SuperButton taskgo10;
    @BindView(R.id.taskrefresh)
    SmartRefreshLayout taskrefresh;

    private NoBackgLodDialog noBackgLodDialog;
    private TaskBean taskBean;
    private PhoneBangding phoneBangding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        ButterKnife.bind(this);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/person-PersonalCenterTask")

                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog == null) {
                            noBackgLodDialog = new NoBackgLodDialog(TaskActivity.this, true);
                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (noBackgLodDialog != null) {
                            noBackgLodDialog.dismis();
                        }
                        taskrefresh.finishRefresh();
                        Gson gson = new Gson();
                        taskBean = gson.fromJson(s, TaskBean.class);
                        if (taskBean.getReturnCode() == 200) {
                            intDataView();
                        } else {
                            showToast(taskBean.getMsg());
                            goBack();
                        }
                    }
                });
    }

    private void intView() {
        taskrefresh.setOnRefreshListener(refreshlayout -> intData());
    }

    private void intDataView() {
        if (taskBean.getData().getPersonalCenterTask().getIsBindingPhoneOK() == 0) {
            taskgo1.setText("去绑定");
        } else {
            taskgo1.setText(getTaskText(taskBean.getData().getPersonalCenterTask().getIsBindingPhoneOK()));
        }
        taskgo2.setText(getTaskText(taskBean.getData().getPersonalCenterTask().getIsAddBookshelfOK()));
        taskgo3.setText(getTaskText(taskBean.getData().getPersonalCenterTask().getIsFollowAuthorOk()));
        taskgo4.setText(getTaskText(taskBean.getData().getPersonalCenterTask().getIsFirstSubscriptionOK()));
        taskgo5.setText(getTaskText(taskBean.getData().getPersonalCenterTask().getIsFirstReward()));
        taskgo6.setText(getTaskDayText(taskBean.getData().getEverydayTasks().getSignOK()));
        taskgo7.setText(getTaskDayText(taskBean.getData().getEverydayTasks().getRecommendTicketOK()));
        taskgo8.setText(getTaskDayText(taskBean.getData().getEverydayTasks().getSubscript()));
        taskgo9.setText(getTaskDayText(taskBean.getData().getEverydayTasks().getGiveThumbs()));
        taskgo10.setText(getTaskDayText(taskBean.getData().getEverydayTasks().getPersonalCenterTaskAllOK()));

    }

    private String getTaskDayText(int i) {
        String tasktext = "未完成";
        switch (i) {
            case 0:
                tasktext = "未完成";
                break;
            case 1:
                tasktext = "已完成";
                break;
        }
        return tasktext;
    }

    @OnClick({R.id.taskback, R.id.taskgo1, R.id.taskgo2, R.id.taskgo3, R.id.taskgo4, R.id.taskgo5})
    public void onViewClicked(View view) {
        if (!UserManageBean.isLogin(TaskActivity.this) && view.getId() != R.id.taskback) {
            return;
        }
        switch (view.getId()) {
            case R.id.taskback:
                goBack();
                break;
            case R.id.taskgo1:
                toclick(taskBean.getData().getPersonalCenterTask().getIsBindingPhoneOK(), 1);
                break;
            case R.id.taskgo2:
                toclick(taskBean.getData().getPersonalCenterTask().getIsAddBookshelfOK(), 2);
                break;
            case R.id.taskgo3:
                toclick(taskBean.getData().getPersonalCenterTask().getIsFollowAuthorOk(), 3);
                break;
            case R.id.taskgo4:
                toclick(taskBean.getData().getPersonalCenterTask().getIsFirstSubscriptionOK(), 4);
                break;
            case R.id.taskgo5:
                toclick(taskBean.getData().getPersonalCenterTask().getIsFirstReward(), 5);
                break;
        }
    }

    private void toclick(int isBindingPhoneOK, int i) {
        switch (isBindingPhoneOK) {
            case 0:
                tofinish(i);
                break;
            case 1:
                toreceive(i);
                break;
            case 2:
                toreceive(i);
                break;
            case 3:
                break;
        }
    }

    private void toreceive(int i) {
        String key = "isBindingPhoneOK";
        switch (i) {
            case 1:
                key = "isBindingPhoneOK";
                break;
            case 2:
                key = "isAddBookshelfOK";

                break;
            case 3:
                key = "isFollowAuthorOk";

                break;
            case 4:
                key = "isFirstSubscriptionOK";

                break;
            case 5:
                key = "isFirstReward";
                break;
        }
        if (taskBean.getData().getPersonalCenterTask().getId().equals("wu")){
            OkHttpUtils.post(HttpManager.IP+"/updatePersonalCenterTaskInfo")

                    .params(key,"3")
                    .execute(new StringCallback() {
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (noBackgLodDialog==null){
                                noBackgLodDialog=new NoBackgLodDialog(TaskActivity.this,true);
                            }
                            noBackgLodDialog.show();
                        }

                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            if (noBackgLodDialog != null) {
                                noBackgLodDialog.dismis();
                            }
                            try {
                                JSONObject jsonObject=new JSONObject(s);
                                if (jsonObject.getInt("returnCode")==200){
                                    UserManageBean.backgroundRiches(TaskActivity.this);
                                    intData();
                                }
                                    showToast(jsonObject.getString("msg"));

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

        }else {
            OkHttpUtils.post(HttpManager.IP+"/updatePersonalCenterTaskInfo")

                    .params(key,"3")
                    .params("id",taskBean.getData().getPersonalCenterTask().getId())
                    .execute(new StringCallback() {
                        @Override
                        public void onBefore(BaseRequest request) {
                            super.onBefore(request);
                            if (noBackgLodDialog==null){
                                noBackgLodDialog=new NoBackgLodDialog(TaskActivity.this,true);
                            }
                            noBackgLodDialog.show();
                        }

                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            if (noBackgLodDialog != null) {
                                noBackgLodDialog.dismis();
                            }
                            try {
                                JSONObject jsonObject=new JSONObject(s);
                                if (jsonObject.getInt("returnCode")==200){
                                    UserManageBean.backgroundRiches(TaskActivity.this);
                                    intData();
                                }
                                showToast(jsonObject.getString("msg"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }

    }

    private void tofinish(int i) {
        switch (i) {
            case 0:
                if (phoneBangding == null) {
                    phoneBangding = new PhoneBangding(TaskActivity.this);
                    phoneBangding.setListener(new PhoneBangding.Listener() {
                        @Override
                        public void giveyanzhengnoLogin() {
                            UserManageBean.isLogin(TaskActivity.this);
                        }

                        @Override
                        public void setok() {
                            UserManageBean.backgroundRiches(TaskActivity.this);
                            intData();
                        }

                        @Override
                        public void clickquhao() {
                            Intent intent = new Intent(TaskActivity.this, SelectquhaoActivity.class);
                            startActivityForResult(intent, CodeManager.Binding_read);
                        }
                    });
                }

                break;
        }
    }


    private String getTaskText(int i) {
        String tasktext = "未完成";
        switch (i) {
            case 0:
                tasktext = "未完成";
                break;
            case 1:
                tasktext = "领取";
                break;
            case 2:
                tasktext = "领取";
                break;
            case 3:
                tasktext = "已完成";
                break;

        }
        return tasktext;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CodeManager.Binding_read && requestCode == CodeManager.Binding_read) {
            if (phoneBangding != null) {
                phoneBangding.setQuhao(data.getStringExtra("countryNumber"));
            }
        }
    }
}